<?php

namespace app\member\controller;

use app\common\controller\Frontend;
use think\Db;
use think\Lang;

/**
 * Ajax异步请求接口
 * @internal
 */
class Ajax extends Frontend
{

    protected $noNeedLogin = ['lang', 'upload','sendsms'];
    protected $noNeedRight = ['*'];
    protected $layout = '';

    /**
     * 加载语言包
     */
    public function lang()
    {
        header('Content-Type: application/javascript');
        header("Cache-Control: public");
        header("Pragma: cache");

        $offset = 30 * 60 * 60 * 24; // 缓存一个月
        header("Expires: " . gmdate("D, d M Y H:i:s", time() + $offset) . " GMT");

        $controllername = input("controllername");
        $this->loadlang($controllername);
        //强制输出JSON Object
        $result = jsonp(Lang::get(), 200, [], ['json_encode_param' => JSON_FORCE_OBJECT | JSON_UNESCAPED_UNICODE]);
        return $result;
    }

    /**
     * 上传文件
     */
    public function upload()
    {
        return action('api/common/upload');
    }
    public function sendsms(){
        $mobile=input("mobile",'','trim');
        $regmobile = "/^1\d{10}$/";
        if(!preg_match($regmobile,$mobile)){
            $this->error("手机号格式不对");

        }
        //开始走发送逻辑
        $statusStr = array(
            "0" => "短信发送成功",
            "-1" => "参数不全",
            "-2" => "服务器空间不支持,请确认支持curl或者fsocket，联系您的空间商解决或者更换空间！",
            "30" => "密码错误",
            "40" => "账号不存在",
            "41" => "余额不足",
            "42" => "帐户已过期",
            "43" => "IP地址限制",
            "50" => "内容含有敏感词"
        );
        $code = rand(1000,9999);
        $smsapi = "http://api.smsbao.com/";
        $user = "1251922916"; //短信平台帐号
        $pass = md5("fjy13053888334"); //短信平台密码
        $content="您的验证码是".$code;//要发送的短信内容
        $phone = "$mobile";//要发送短信的手机号码
        $sendurl = $smsapi."sms?u=".$user."&p=".$pass."&m=".$phone."&c=".urlencode($content);
        $result =file_get_contents($sendurl) ;
        if($result==="0"||$result===0){
            Db::name('smscode')->insert([
                'code'=>$code,
                'mobile'=>$mobile,
                'createtime'=>time()
            ]);
            $this->success('发送短信成功了');
            //走完这一步不再向下执行
        }
        else{
            $this->error($statusStr[$result]);
        }


    }

}
